package com.tan.lighting.disruptor.factory;


import com.lmax.disruptor.SleepingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;
import com.lmax.disruptor.util.DaemonThreadFactory;
import com.tan.lighting.disruptor.event.CommandEvent;
import com.tan.lighting.disruptor.event.factory.CommandEventFactory;
import com.tan.lighting.disruptor.handler.ExcuteCommandHandlerCommand;
import com.tan.lighting.disruptor.handler.JournalCommandHandler;
import com.tan.lighting.disruptor.handler.ReplicateCommandHandler;

/**
 * Created by tan on 2017/6/23.
 */
public class DisruptorFactory {
     public static Disruptor<CommandEvent> createDisruptor(JournalCommandHandler journal, ReplicateCommandHandler replicate, ExcuteCommandHandlerCommand excute) throws Exception {
        Disruptor<CommandEvent> disruptor = new Disruptor<CommandEvent>(new CommandEventFactory(), 1024 * 1024,
                DaemonThreadFactory.INSTANCE, ProducerType.MULTI, new SleepingWaitStrategy());
        disruptor.handleEventsWith(journal,replicate).then(excute);
        disruptor.start();
        return disruptor;
    }
}
